<template>
	<view class="container">
		<!-- 背景蓝色区域 -->
		<view class="bg-blue"></view>

		<!-- 卡片区域 -->
		<view class="card">
			<!-- 用户信息部分 -->
			<view class="user-info">
				<!-- 头像 -->
				<view class="avatar">
					<u-avatar :src="avatarUrl" size="70" shape="circle"></u-avatar>
				</view>

				<!-- 用户名称和手机号 -->
				<view class="user-text">
					<view v-if="isLogin || isDataReset">
						<text class="username">{{ userName || '用户' }}</text>
						<text class="phone">{{ phone || '188****1234' }}</text>
					</view>
					<view v-else>
						<text class="login-tip" @click="goToLogin">点击登录</text>
					</view>
				</view>

				<!-- 设置图标 -->
				<view class="settings-container" v-if="isLogin || isDataReset">
					<!-- 设置图标 -->
					<u-icon name="setting-fill" color="#0079FE" size="28" class="settings-icon"
						@click="show = true"></u-icon>

					<!-- 辅助模块 - 右侧弹出层 -->
					<u-popup v-model="show" mode="right" class="settings-popup">
						<view class="popup-content">
							<view class="popup-header" @click="goToUserInfo">
								<text class="popup-title">个人中心</text>
							</view>

							<view class="popup-item" hover-class="popup-item-hover" @click="loginOut">
								<text class="popup-item-text">退出登录</text>
								<u-icon name="arrow-right" size="16" color="#999"></u-icon>
							</view>
						</view>
					</u-popup>
				</view>
			</view>

			<!-- 统计信息部分 -->
			<view class="stats">
				<view class="stat-item">
					<text class="stat-num">{{ followNum }}</text>
					<text class="stat-label">关注</text>
				</view>
				<view class="stat-divider"></view>
				<view class="stat-item">
					<text class="stat-num">{{ couponsNum }}</text>
					<text class="stat-label">优惠券</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import token from '../../utils/token';
	export default {
		data() {
			return {
				avatarUrl: '', // 替换为实际头像URL
				userName: '',
				phone: '',
				followNum: 0, //关注数量
				couponsNum: 0, //优惠卷数量
				show: false,
				isDataReset: false, // 新增状态，用于控制是否已重置数据
			};
		},
		computed: {
			isLogin() {
				return !!uni.getStorageSync('isLogin'); // 确保返回布尔值
			}
		},
		watch: {
			isLogin(newVal) {
				console.log(newVal)
				if (!newVal) {
					this.resetUserData();
					this.isDataReset = true; // 标记数据已重置
				}
			}
		},
		async onShow() {
			//console.log("123", uni.getStorageSync('isLogin'))
			// 页面显示时检查登录状态
			if (uni.getStorageSync('isLogin')) {
				await this.loadUserData(); //每次进入页面都刷新数据
			}
		},
		created() {
			this.loadUserData()
		},
		methods: {
			//用户个人中心
			goToUserInfo() {
				uni.navigateTo({
					url: '/pages/mine/userInfo'
				})
			},
			//数据渲染
			loadUserData() {
				this.userName = this.$store.state.userName
				this.phone = this.$store.state.userPhoneNumber
				this.avatarUrl = this.$store.state.userAvatar
			},
			//跳转登录界面
			goToLogin() {
				uni.navigateTo({
					url: '/pages/login/login'
				});
			},
			//重置数据
			resetUserData() {
				this.followNum = 0;
				this.couponsNum = 0;
				this.show = false;
				this.userName = '';
				this.phone = '';
			},
			//退出登录
			loginOut() {
				token.removeToken('tokenTestHealth');
				uni.removeStorageSync('isLogin');
				this.resetUserData();

				uni.reLaunch({
					url: '/pages/index/index',
					success() {
						// 强制重新加载页面（适用于 H5）
						// #ifdef H5
						window.location.reload();
						// #endif
					}
				});
			}
		}
	};
</script>

<style lang="scss" scoped>
	.container {
		position: relative;
		padding: 30rpx;
		background-color: #F2F2F2;

		.bg-blue {
			position: absolute;
			top: 0;
			left: 0;
			right: 0;
			height: 300rpx;
			background-color: #0079FE;
			z-index: 0;
		}

		.card {
			position: relative;
			z-index: 1;
			background-color: #ffffff;
			padding: 30rpx;
			margin-top: 40rpx;
			box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.08);
		}

		.user-info {
			display: flex;
			align-items: center;
			margin-bottom: 40rpx;

			.avatar {
				margin-right: 20rpx;
			}

			.user-text {
				flex: 1;

				.username {
					display: block;
					font-size: 18px;
					/* 从16px增大到18px */
					font-weight: bold;
					color: #333;
					margin-bottom: 8rpx;
				}

				.phone {
					font-size: 15px;
					color: #999;
				}
			}

			.settings {
				padding: 12rpx;
			}
		}

		.stats {
			display: flex;
			align-items: center;

			.stat-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				flex: 1;

				.stat-num {
					font-size: 20px;
					font-weight: bold;
					color: #333;
					margin-bottom: 8rpx;
				}

				.stat-label {
					font-size: 13px;
					color: #999;
				}
			}

			.stat-divider {
				width: 1rpx;
				height: 36rpx;
				background-color: #eee;
				margin: 0 30rpx;
			}
		}

		/* 弹窗整体样式 */
		.u-popup {
			background-color: #ffffff !important;
			height: 100vh;
			width: 70vw;
			padding: 40rpx 0;
			box-shadow: -10rpx 0 30rpx rgba(0, 0, 0, 0.1);
		}

		/* 弹窗内容区域 */
		.u-popup__content {
			display: flex;
			flex-direction: column;
			height: 100%;
		}

		/* 按钮通用样式 */
		button {
			background-color: transparent;
			padding: 30rpx 40rpx;
			text-align: left;
			font-size: 32rpx;
			color: #333;
			border: none;
			border-radius: 0;
			transition: all 0.3s;

			&:active {
				background-color: #f5f5f5;
				opacity: 0.8;
			}
		}

		/* 设置容器样式 */
		.settings-container {
			position: relative;
			display: flex;
			align-items: center;
			justify-content: flex-end;
			padding: 10px;
		}

		/* 设置图标样式 */
		.settings-icon {
			padding: 8px;
			border-radius: 50%;
			background-color: #f5f5f5;
			transition: all 0.3s;
		}

		.settings-icon:active {
			background-color: #e6e6e6;
			transform: scale(0.95);
		}

		/* 弹出层内容样式 */
		.popup-content {
			width: 70vw;
			height: 100%;
			background-color: #fff;
			padding: 20px 0;
		}

		/* 弹出层头部 */
		.popup-header {
			padding: 0 20px 20px;
			border-bottom: 1px solid #f5f5f5;
		}

		.popup-title {
			font-size: 18px;
			font-weight: 500;
			color: #333;
		}

		/* 弹出层菜单项 */
		.popup-item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding: 14px 20px;
			margin: 8px 0;
			transition: all 0.3s;
		}

		.popup-item-hover {
			background-color: #f5f5f5;
		}

		.popup-item-text {
			font-size: 16px;
			color: #333;
		}
	}
</style>